//
//  TollPicturesCell.m
//  Runone
//
//  Created by runone02 on 16/9/28.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "TollPicturesCell.h"
#import "ROPhotoBrowser.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "MJExtension.h"

@interface TollPicturesCell ()<ROPhotoBrowserDelegate>

@property (nonatomic, strong)PicturesView *picView;
@property (nonatomic, strong)UIView *containerV;

@property (nonatomic, strong) NSMutableArray *mPicArray;//图片数组

@end

@implementation TollPicturesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"cellID";
    id cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor clearColor];
        _mPicArray = [[NSMutableArray alloc] init];
        [self configUI];
    }
    return self;
}
//配置UI界面
- (void) configUI {
    __weak __typeof(self)weakSelf = self;
    _containerV = [[UIView alloc] init];
    [self addSubview:_containerV];
    
    [_containerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mas_top);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
    }];
    
    for (int i = 0; i < 9; i++) {
        UIImageView *imageV  = [[UIImageView alloc] init];
        imageV.contentMode   = UIViewContentModeScaleAspectFill;
        imageV.hidden        = YES;
        imageV.clipsToBounds = YES;
        [_containerV addSubview:imageV];
        imageV.userInteractionEnabled = YES;
        imageV.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [imageV addGestureRecognizer:tap];
        [_mPicArray addObject:imageV];
    }
}

#pragma mark - tap手势
- (void)tapImageView:(UITapGestureRecognizer *)tap {
    UIView *imageView = tap.view;
    ROPhotoBrowser *browser   = [[ROPhotoBrowser alloc] init];
    browser.isAnnimation      = YES;
    browser.currentImageIndex = imageView.tag;
    browser.sourceImagesContainerView = self.containerV;
    browser.imageCount = _picturesArray.count;
    browser.delegate   = self;
    [browser show];
}

- (void)setPicturesArray:(NSArray *)picturesArray {
    _picturesArray = picturesArray;
    if (_picturesArray.count == 0) {
        return;
    }
    int count = (int)_picturesArray.count;
    for (int i = 0; i < count; i++) {
        
        UIImageView *imageV = _mPicArray[i];
        
        if (i < _picturesArray.count) {
            imageV.hidden = NO;
            NSString *picUrl = _picturesArray[i];
            [imageV sd_setImageWithURL:[picUrl mj_url]];
        }else {
            imageV.hidden = YES;
        }
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    CGFloat x =  8;
    CGFloat y =  0;
    CGFloat w = self.bounds.size.width - 16;
    CGFloat h = 200;
    CGFloat margin = 8;
    for (int i = 0; i < _picturesArray.count; i++) {
        UIImageView *imageV = _mPicArray[i];
        y = i * (h + margin) + margin;
        imageV.frame = CGRectMake(x, y, w, h);
    }
}

#pragma mark -
- (void)prepareForReuse {
    [super prepareForReuse];
}

#pragma mark - ROPhotoBrowserDelegate
- (NSURL *)photoBrowser:(ROPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    NSString *imageName = _picturesArray[index];
    NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
    return url;
}

- (UIImage *)photoBrowser:(ROPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    UIImageView *imageView = _mPicArray[index];
    return imageView.image;
}


@end
